package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ThreadPoolExecutor executor;
    @Qualifier("com.atguigu.tingshu.album.AlbumFeignClient")
    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Qualifier("com.atguigu.tingshu.user.client.UserFeignClient")
    @Autowired
    private UserFeignClient userFeignClient;


    @Override
    public Map<String, Object> getItem(Long albumId) {
        HashMap<String, Object> resultMap = new HashMap<>();
        //获取albuminfo信息
        CompletableFuture<AlbumInfo> albuminfoFuture = CompletableFuture.supplyAsync(
                () -> {
                    AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
                    resultMap.put("albumInfo",albumInfo);
                    return  albumInfo;
                }, executor);

       //获取用户信息
        CompletableFuture<Void> userInfoVoFuture = albuminfoFuture.thenAcceptAsync(
                albumInfo ->
                {
                    UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
                    resultMap.put("announcer", userInfoVo);

                }, executor);

        //获取分类信息
        CompletableFuture<Void> categoryFuture = albuminfoFuture.thenAcceptAsync(albumInfo ->
        {
            BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            resultMap.put("baseCategoryView", categoryView);

        }, executor);


        CompletableFuture<Void> albumStatFuture = albuminfoFuture.runAsync(() -> {

            AlbumStatVo albumStat = albumFeignClient.getAlbumStatVo(albumId).getData();
            resultMap.put("albumStatVo", albumStat);
        }, executor);


        CompletableFuture.allOf(albuminfoFuture,userInfoVoFuture, categoryFuture, albumStatFuture).join();

        return  resultMap;

    }
}
